/***********************************************************************************************************************
*                                                                                                                      *
* SPLASH build system v0.1                                                                                             *
*                                                                                                                      *
* Copyright (c) 2013 Andrew D. Zonenberg                                                                               *
* All rights reserved.                                                                                                 *
*                                                                                                                      *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the     *
* following conditions are met:                                                                                        *
*                                                                                                                      *
*    * Redistributions of source code must retain the above copyright notice, this list of conditions, and the         *
*      following disclaimer.                                                                                           *
*                                                                                                                      *
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the       *
*      following disclaimer in the documentation and/or other materials provided with the distribution.                *
*                                                                                                                      *
*    * Neither the name of the author nor the names of any contributors may be used to endorse or promote products     *
*      derived from this software without specific prior written permission.                                           *
*                                                                                                                      *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES        *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR       *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE       *
* POSSIBILITY OF SUCH DAMAGE.                                                                                          *
*                                                                                                                      *
***********************************************************************************************************************/

#ifndef CppToolchain_h
#define CppToolchain_h

#include "Cluster.h"

class CppToolchain;

typedef BuildFlag<CppToolchain> CppLinkFlag;
typedef BuildFlag<CppToolchain> CppCompileFlag;

typedef BuildFlagList<CppCompileFlag> CppCompileFlagList;
typedef BuildFlagList<CppLinkFlag> CppLinkFlagList;

/**
	@brief Generic representation of a C++ compiler
 */
class CppToolchain : public Toolchain
{
public:
	CppToolchain();
	virtual ~CppToolchain();

	/**
		@brief Caching wrapper around CreateDefaultToolchain(), never calls it more than once.
	 */
	static CppToolchain* CreateDefaultToolchainCached();
	
	/**
		@brief Caching wrapper around CreateDefaultToolchainForArchitecture(), never calls it more than once.
	 */
	static CppToolchain* CreateDefaultToolchainForArchitectureCached(std::string arch);
	
	///@brief Returns a human-readable description of the toolchain (ex: "g++ (Debian 4.7.2-5) 4.7.2")
	virtual std::string GetDescription() const =0;
	
	///@brief Returns a SHA256 hash uniquely identifying this toolchain version
	virtual std::string GetHash() const =0;
	
	///@brief Returns the hash of the provided compile flags
	virtual std::string GetCflagsHash(const CppCompileFlagList& flags) const =0;
	
	///@brief Returns the hash of the provided link flags
	virtual std::string GetLflagsHash(const CppLinkFlagList& flags) const =0;
	
	///@brief Appends compiler-specific include paths to a vector of directories
	virtual void GetCompilerIncludePaths(std::vector<std::string>& paths) =0;
	
	///@brief Gets the list of macros defined by default
	virtual void GetDefaultMacros(const CppCompileFlagList& flags, std::map<std::string, std::string>& macros) =0;
	
	///@brief Compile a single source file to an object file.
	virtual ClusterJob* CompileSourceToObject(
		std::string source_filename,
		std::string object_filename,
		const CppCompileFlagList& flags,
		const std::vector<ClusterJob*>& deps,
		Cluster* cluster,
		std::string project_root
		) =0;
	
	///@brief Link one or more object files to an executable.
	virtual ClusterJob* LinkObjectsToExecutable(
		const std::vector<std::string>& objects,
		std::string exe_filename,
		const CppLinkFlagList& flags,
		const std::vector<ClusterJob*>& deps,
		Cluster* cluster
		) =0;
	
	///@brief Link one or more object files to a shared library.
	virtual ClusterJob* LinkObjectsToSharedLibrary(
		const std::vector<std::string>& objects,
		std::string exe_filename,
		const CppLinkFlagList& flags,
		const std::vector<ClusterJob*>& deps,
		Cluster* cluster
		) =0;

	///@brief Link one or more object files to a static library.
	virtual ClusterJob* LinkObjectsToStaticLibrary(
		const std::vector<std::string>& objects,
		std::string exe_filename,
		const CppLinkFlagList& flags,
		const std::vector<ClusterJob*>& deps,
		Cluster* cluster
		) =0;
		
	///@brief Dumps a named set of sections from an executable file
	virtual ClusterJob* DumpSectionsFromExecutable(
		std::string exe_filename,
		std::string bin_filename,
		std::vector<std::string> sections,
		const std::vector<ClusterJob*>& deps,
		Cluster* cluster
		) =0;
	
	///@brief Gets the architecture triplet this compiler is targeting
	virtual std::string GetArchitecture() const =0;
	
	static std::map<std::string, CppToolchain*>::const_iterator begin()
	{ return m_cachedToolchains.begin(); }
	
	static std::map<std::string, CppToolchain*>::const_iterator end()
	{ return m_cachedToolchains.end(); }
	
	virtual std::string GetExecutableSuffix() const =0;
	virtual std::string GetStaticLibrarySuffix() const =0;
	virtual std::string GetSharedLibrarySuffix() const =0;
	
protected:
	
	/**
		@brief Creates a CppToolchain object for a best-guess compiler on the host machine.
		
		This may not be the most efficient or optimized one, but it's very likely to work.
	 */
	static CppToolchain* CreateDefaultToolchain();
	
	/**
		@brief Creates a CppToolchain object for a best-guess compiler on the target architecture.
		
		This may not be the most efficient or optimized one, but it's very likely to work.
	 */
	static CppToolchain* CreateDefaultToolchainForArchitecture(std::string arch);
	
	static std::map<std::string, CppToolchain*> m_cachedToolchains;
};

#include "CppCompileFlags.h"
#include "CppLinkFlags.h"

#endif
